﻿using AutoMapper;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Project.Core.Model;
using Project.Core.Api.Common.Base;

namespace Project.Core.Api.AutoMapper
{
    /// <summary>
    /// BaseMapping
    /// </summary>
    public class BaseMapping : IBaseMapping
    {
        /// <summary>
        /// Mapping
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        [MethodImpl(MethodImplOptions.Synchronized)]
        public static TModel Mapping<TEntity, TModel>(TEntity source) where TEntity : BaseModel where TModel : BaseDto, new()
        {
            if (source == null)
                return new TModel();

            Mapper.Initialize(cfg => cfg.CreateMap<TEntity, TModel>());

            return Mapper.Map<TModel>(source);
        }
    }
}
